package com.japp.manager.service;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.japp.base.BaseService;
import com.japp.manager.bean.QtargetBean;
import com.japp.manager.bean.RealityQtargetBean;
import com.japp.manager.mapper.QtargetMapper;
import com.japp.tools.common.Common;
import com.japp.tools.common.JappLog;
@Service
public class RealityQtargetService extends SqlSessionDaoSupport implements BaseService{

	@Autowired
	private SqlSessionTemplate template;
	@Autowired
	private QtargetMapper qtargetMapper;

	@Transactional(readOnly = true)
	public Map<String, Object> findRealityQ(RealityQtargetBean q) {
		Map<String, Object> map = new HashMap<String, Object>();
		if ("2".equals(q.getQ_date_type())) {
			q.setQ_cdate(String.valueOf(Common.getThisDateWEEK(q.getQ_date())));
		}
		String date = q.getQ_date();
		String cdate = q.getQ_cdate();
		String date_type = q.getQ_date_type();
		String power = q.getQ_power();
		String dpid = q.getQ_dpid();
		String ucode = q.getQ_ucode();
		if ("1".equals(power)) {
			dpid = "";
			ucode = "";
		} else if ("2".equals(power)) {
			ucode = "";
		}
		power = "";
		Connection conn = null;
		CallableStatement call = null;
		ResultSet rs = null;
		try {
			conn = template.getSqlSessionFactory().openSession().getConnection();
			/******正常数据开始******/
			JappLog.log.info("正常数据: 目标考核数据开始");
			//经营数据
			call = conn.prepareCall("{CALL PRO_REALITY_Q(?,?,?,?,?,?)}");
			call.setString(1, date_type);
			call.setString(2, date);
			call.setString(3, cdate);
			call.setString(4, dpid);
			call.setString(5, ucode);
			call.registerOutParameter(6, oracle.jdbc.OracleTypes.CURSOR);
			call.execute();
			rs = (ResultSet) call.getObject(6);
			if (rs.next()) {
				q = (RealityQtargetBean) Common.getObjView(q, rs);
			}
			map.put("r",q);
			/******年度统计开始******/
			JappLog.log.info("年度统计: 目标考核数据开始");
			q = new RealityQtargetBean();
			q.setQ_date(date);
			q.setQ_cdate(cdate);
			q.setQ_date_type(date_type);
			q.setQ_dpid(dpid);
			q.setQ_power(power);
			q.setQ_ucode(ucode);
			call.close();
			call = null;
			call = conn.prepareCall("{CALL PRO_REALITY_Q(?,?,?,?,?,?)}");
			call.setString(1, date_type);
			call.setString(2, date);
			call.setString(3, "ALL");
			call.setString(4, dpid);
			call.setString(5, ucode);
			call.registerOutParameter(6, oracle.jdbc.OracleTypes.CURSOR);
			call.execute();
			rs = (ResultSet) call.getObject(6);
			if (rs.next()) {
				q = (RealityQtargetBean) Common.getObjView(q, rs);
			}
			map.put("c",q);
			/******年度统计结束******/
			/******计划开始******/
			JappLog.log.info("计划数据: 目标考核数据开始");
			QtargetBean qb = new QtargetBean();
			qb.setT_type(date_type);
			qb.setT_submit_date(date);
			qb.setT_submit_cdate(cdate);
			qb.setT_submit_ucode(ucode);
			qb.setT_submit_dpid(dpid);
			qb.setT_power(power);
			for (int i = Integer.valueOf(date_type); i < 7; i++) {
				qb.setT_type(String.valueOf(i));
				if(i==4){
					qb.setT_submit_cdate(Common.getThisDateJdStart(cdate,date));
				}else if(i==5){
					qb.setT_submit_cdate(Common.getThisDateYear1(date));
				}
				map.put("en_"+i, qtargetMapper.findQtargetByBean(qb));
			}
			/******计划结束******/
			/*****上期数查询开始*****/
			JappLog.log.info("正常上期数据: 目标考核数据开始");
			q = new RealityQtargetBean();
			q.setQ_date(date);
			q.setQ_cdate(cdate);
			q.setQ_date_type(date_type);
			q.setQ_dpid(dpid);
			q.setQ_power(power);
			q.setQ_ucode(ucode);
			//判断开始
			//判断开始
			if("1".equals(date_type)){
				date = Common.getThisDateYMD(date, "yyyy-MM-dd", "D", -1);
			}else if("2".equals(date_type)){
				date = Common.getThisDateYMD(date, "yyyy-MM-dd", "IW", -1);
				cdate = String.valueOf(Common.getThisDateWEEK(date));
			}else if("3".equals(date_type)){
				date = Common.getThisDateYMD(date, "yyyy-MM", "M", -1);
			}else if("4".equals(date_type)){
				if("1".equals(cdate)){
					cdate = "4";
					date = (date.length()==4?(date+"-01-01"):date);
					date = Common.getThisDateYMD(date, "yyyy-MM-dd", "Y", -1);
				}else{
					date = date.substring(0,4)+Common.getThisDateJdStart(cdate,date).substring(4,10);
					cdate = String.valueOf(Integer.valueOf(cdate)-1);
				}
			}else if("5".equals(date_type)){
				if("1".equals(cdate)){
					cdate = "2";
					date = Common.getThisDateYMD(date.substring(0,4), "yyyy", "Y", -1);
				}else{
					cdate = "1";
				}
			}else if("6".equals(date_type)){
				date = Common.getThisDateYMD(date.substring(0,4), "yyyy", "Y", -1);
			}
			//经营数据
			call.close();
			call = null;
			call = conn.prepareCall("{CALL PRO_REALITY_Q(?,?,?,?,?,?)}");
			call.setString(1, date_type);
			call.setString(2, date);
			call.setString(3, cdate);
			call.setString(4, dpid);
			call.setString(5, ucode);
			call.registerOutParameter(6, oracle.jdbc.OracleTypes.CURSOR);
			call.execute();
			rs = (ResultSet) call.getObject(6);
			if (rs.next()) {
				q = (RealityQtargetBean) Common.getObjView(q, rs);
			}
			map.put("r-1", q);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return map;
	}
}
